package com.yisu.expressway.manager.core.service.impl;

import com.yisu.expressway.ewf.entity.CategoryBanner;
import com.yisu.expressway.manager.core.mapper.CategoryBannerMapper;
import com.yisu.expressway.manager.core.service.ICategoryBannerService;
import com.yisu.expressway.manager.domain.request.CategoryBannerRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xlt on 2017/4/26.
 */
@Service
public class CategoryBannerServiceImpl implements ICategoryBannerService {
    @Resource
    private CategoryBannerMapper categoryBannerMapper;

    /**
     * 编辑分类banner信息
     *
     * @param categoryBanner 分类banner信息
     * @return 成功失败
     */
    @Override
    public boolean modifyCategoryBanner(CategoryBanner categoryBanner) {
        try {
            CategoryBanner entity = categoryBannerMapper.selectByPrimaryKey(categoryBanner.getId());
            if (entity == null) {
                categoryBannerMapper.insert(categoryBanner);
            } else {
                categoryBannerMapper.updateByPrimaryKey(categoryBanner);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 通过分类编号，获取banner列表信息
     *
     * @param categoryBannerRequest
     * @return 列表信息
     */
    @Override
    public List<CategoryBanner> getCategoryBannerByCategoryCode(CategoryBannerRequest categoryBannerRequest) {
        return categoryBannerMapper.getCategoryBannerByCategoryCode(categoryBannerRequest);
    }

    /**
     * 通过主键删除banner信息
     *
     * @param primaryKey 主键
     * @return 成功，失败
     */
    @Override
    public boolean deleteCategoryBannerByPrimaryKey(Long primaryKey) {
        return categoryBannerMapper.deleteByPrimaryKey(primaryKey)>0;
    }

    /**
     * 通过主键获取banner信息
     *
     * @param primaryKey
     * @return banner信息
     */
    @Override
    public CategoryBanner getCategoryBannerModel(Long primaryKey) {
        return categoryBannerMapper.selectByPrimaryKey(primaryKey);
    }

    /**
     * 更新banner排序值
     *
     * @param categoryBanner 分类信息
     * @return 成功，失败
     */
    @Override
    public boolean updateSort(CategoryBanner categoryBanner) {
        return categoryBannerMapper.updateSort(categoryBanner)>0;
    }

    /**
     * 获取分类下banner的最大排序
     *
     * @param categoryBannerRequest 请求参数 分类ID
     * @return 排序值
     */
    @Override
    public int getMaxSortByCategoryCode(CategoryBannerRequest categoryBannerRequest) {
        return categoryBannerMapper.getMaxSortByCategoryCode(categoryBannerRequest);
    }
}
